package com.yuantong.xyerp.core.provider.dao;

import com.yuantong.xyerp.core.api.dto.UserDto;
import com.yuantong.xyerp.core.api.model.User;
import com.yuantong.xyerp.core.api.model.UserDetail;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author JianguoYin
 * @since 2021-06-24
 */
public interface UserMapper {


    @Insert({"insert into t_user (id, real_name, organization_id, role_id, login_name, password, mobile, gender," +
            " `position`, rank, entry_date, sfz_no, nation, marriage, education, emergency_contact, emergency_mobile," +
            " household_register, address, update_time, create_time, status, `delete_status`) " +
            "VALUES (#{id}, #{realName}, #{organizationId}, #{roleId}, #{loginName}, #{password}, #{mobile}, " +
            "#{gender}, #{position}, #{rank}, #{entryDate}, #{sfzNo}, #{nation}, #{marriage}, #{education}, " +
            "#{emergencyContact}, #{emergencyMobile}, #{householdRegister}, #{address}, #{updateTime}, " +
            "#{createTime}, #{status}, #{deleteStatus})"})
    @Options(useGeneratedKeys = false)
    int addUser(User user);

    // <foreach collection="userList" item="user" separator="," >
    // </foreach>
    // on duplicate key update  针对unique的column, 当新增时他们发生了重复情况, 就会找到重复的那一行数据进行更新

    /**
     * 通过Dto导入
     * @param userDtos
     * @return
     */
    @Insert({"<script> " +
            "INSERT INTO t_user (id, real_name, organization_id, role_id, login_name, password, mobile, gender, " +
            "`position`, rank, entry_date, sfz_no, nation, marriage, education, emergency_contact, emergency_mobile, " +
            "household_register, address, update_time, create_time, status, `delete_status`) " +
            "VALUES " +
            "<foreach collection=\"userDtos\" item=\"userDto\" separator=\",\" > " +
            "(#{userDto.id}, #{userDto.realName}, #{userDto.organizationId}, #{userDto.roleId}, " +
            "#{userDto.loginName}, #{userDto.password}, #{userDto.mobile}, #{userDto.gender}, #{userDto.position}, " +
            "#{userDto.rank}, #{userDto.entryDate}, #{userDto.sfzNo}, #{userDto.nation}, #{userDto.marriage}, " +
            "#{userDto.education}, #{userDto.emergencyContact}, #{userDto.emergencyMobile}, " +
            "#{userDto.householdRegister}, #{userDto.address}, #{userDto.updateTime}, #{userDto.createTime}, " +
            "#{userDto.status}, #{userDto.deleteStatus}) " +
            "</foreach> " +
            "ON DUPLICATE KEY UPDATE " +
            "real_name = VALUES(real_name), organization_id = VALUES(organization_id), " +
            "role_id = VALUES(role_id), login_name = VALUES(login_name), password = VALUES(password), " +
            "mobile = VALUES(mobile), gender = VALUES(gender), `position` = VALUES(`position`), rank = VALUES(rank), " +
            "entry_date = VALUES(entry_date), sfz_no = VALUES(sfz_no), nation = VALUES(nation), " +
            "marriage = VALUES(marriage), education = VALUES(education), emergency_contact = VALUES(emergency_contact), " +
            "emergency_mobile = VALUES(emergency_mobile), household_register = VALUES(household_register), " +
            "address = VALUES(address), update_time = VALUES(update_time), create_time = VALUES(create_time), " +
            "status = VALUES(status), `delete_status` = VALUES(`delete_status`) " +
            "</script>"})
    @Options(useGeneratedKeys = false)
    int saveBatchByUserDtos(List<UserDto> userDtos);

    @Update({"<script>" +
            "        UPDATE t_user" +
            "        <set>" +
            "            <if test=\"realName != null\">" +
            "                real_name = #{realName}," +
            "            </if>" +
            "            <if test=\"organizationId != null\">" +
            "                organization_id = #{organizationId}," +
            "            </if>" +
            "            <if test=\"roleId != null\">" +
            "                role_id = #{roleId}," +
            "            </if>" +
            "            <if test=\"loginName != null\">" +
            "                login_name = #{loginName}," +
            "            </if>" +
            "            <if test=\"password != null\">" +
            "                password = #{password}," +
            "            </if>" +
            "            <if test=\"mobile != null\">" +
            "                mobile = #{mobile}," +
            "            </if>" +
            "            <if test=\"gender != null\">" +
            "                gender = #{gender}," +
            "            </if>" +
            "            <if test=\"position != null\">" +
            "                `position` = #{position}," +
            "            </if>" +
            "            <if test=\"rank != null\">" +
            "                rank = #{rank}," +
            "            </if>" +
            "            <if test=\"entryDate != null\">" +
            "                entry_date = #{entryDate}," +
            "            </if>" +
            "            <if test=\"sfzNo != null\">" +
            "                sfz_no = #{sfzNo}," +
            "            </if>" +
            "            <if test=\"nation != null\">" +
            "                nation = #{nation}," +
            "            </if>" +
            "            <if test=\"marriage != null\">" +
            "                marriage = #{marriage}," +
            "            </if>" +
            "            <if test=\"education != null\">" +
            "                education = #{education}," +
            "            </if>" +
            "            <if test=\"emergencyContact != null\">" +
            "                emergency_contact = #{emergencyContact}," +
            "            </if>" +
            "            <if test=\"emergencyMobile != null\">" +
            "                emergency_mobile = #{emergencyMobile}," +
            "            </if>" +
            "            <if test=\"householdRegister != null\">" +
            "                household_register = #{householdRegister}," +
            "            </if>" +
            "            <if test=\"address != null\">" +
            "                address = #{address}," +
            "            </if>" +
            "            <if test=\"updateTime != null\">" +
            "                update_time = #{updateTime}," +
            "            </if>" +
            "            <if test=\"createTime != null\">" +
            "                create_time = #{createTime}," +
            "            </if>" +
            "            <if test=\"status != null\">" +
            "                status = #{status}," +
            "            </if>" +
            "            <if test=\"deleteStatus != null\">" +
            "                `delete_status` = #{deleteStatus}," +
            "            </if>" +
            "        </set>" +
            "        WHERE id = #{id}" +
            "</script>"})
    int update(User user);

    /**
     * 根据原型得到的 Dto
     *
     * 用于 根据选择栏条件全部导出
     *
     * 可用于 根据原型左边的复选框有选择的导出, 导出用 <foreach>里的对象含有unique属性的字段就可以, 这里拟定传主表 id
     * @param roleId
     * @param organizationId
     * @param fuzzy
     * @param userDtos
     * @return
     */
    @Select({"<script>" +
            "SELECT u.*, r.name AS role_name, o.name AS organization_name FROM t_user u " +
            "LEFT JOIN t_role r ON (r.id = u.role_id AND r.status = 1) " +
            "LEFT JOIN t_organization o ON (o.id = u.organization_id AND o.status = 1) " +
            "WHERE 1 = 1 " +
            "<if test=\"organizationId != null and organizationId != ''\"> " +
            "AND u.organization_id = #{organizationId}  " +
            "</if> " +
            "<if test=\"roleId != null and roleId != ''\"> " +
            "AND u.role_id = #{roleId}  " +
            "</if> " +
            "<if test=\"fuzzy != null and fuzzy != ''\"> " +
            "<bind name=\"bindKey\" value=\"'%'+fuzzy+'%'\"/> " +
            "AND (u.real_name LIKE #{bindKey} OR o.name LIKE #{bindKey} OR r.name LIKE #{bindKey}) " +
            "</if> " +
            "<if test=\"userDtos != null and userDtos.size() > 0\"> " +
            "AND u.id IN " +
            "<foreach collection=\"userDtos\" item=\"userDto\" open=\"(\" close=\")\" separator=\",\"> " +
            "#{userDto.id} " +
            "</foreach> " +
            "</if> " +
            "ORDER BY u.create_time DESC " +
            "</script>"})
    @ResultMap("UserDtoResult")
    List<UserDto> findByFuzzy(String roleId, String organizationId, String fuzzy, List<UserDto> userDtos);

    /**
     * 根据userDto 的 organizationId
     * 查找 该部门 的 所有员工
     * @param userDto
     * @return
     */
    @Select({"<script>" +
            "SELECT u.*, r.name AS role_name, o.name AS organization_name FROM t_user u " +
            "LEFT JOIN t_role r ON (r.id = u.role_id AND r.status = 1) " +
            "LEFT JOIN t_organization o ON (o.id = u.organization_id AND o.status = 1) " +
            "WHERE 1 = 1 " +
            "<if test=\"userDto != null \"> " +
            "   AND u.organization_id = #{userDto.organizationId}  " +
            "</if> " +
            "</script>"})
    @ResultMap("UserDtoResult")
    List<UserDto> findByOrganizationId(@Param("userDto") UserDto userDto);

    /**
     * 根据userDto 的 roleId
     * 查找 该角色下 的 所有员工
     * @param userDto
     * @return
     */
    @Select({"<script>" +
            "SELECT u.*, r.name AS role_name, o.name AS organization_name FROM t_user u " +
            "LEFT JOIN t_role r ON (r.id = u.role_id AND r.status = 1) " +
            "LEFT JOIN t_organization o ON (o.id = u.organization_id AND o.status = 1) " +
            "WHERE 1 = 1 " +
            "<if test=\"userDto != null \"> " +
            "   AND u.role_id = #{userDto.roleId}  " +
            "</if> " +
            "</script>"})
    @ResultMap("UserDtoResult")
    List<UserDto> findByRoleId(@Param("userDto") UserDto userDto);


    /**
     * 传给前端的subject详情
     * @param userDetail
     * @return
     */
    @Select({"<script> " +
            "SELECT u.*, o.name AS organization_name, r.name AS role_name,r.data_rights, " +
            "ma.id AS menu_id, ma.code AS menu_code, ma.name AS menu_name, ma.pid AS menu_pid, ma.status AS menu_status " +
            "FROM t_user u " +
            "LEFT JOIN t_organization o ON o.id = u.organization_id " +
            "LEFT JOIN t_role r ON r.id = u.role_id " +
            "LEFT JOIN t_role_menu rm ON rm.role_id = u.role_id " +
            "LEFT JOIN t_menu_auth ma ON ( rm.menu_id = ma.id  ) " +
            "WHERE u.login_name=#{loginName} " +
            "</script>"})
    @ResultMap("UserDetailResult")
    UserDetail selectOneByUserDetail(UserDetail userDetail);


}
